import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import { getGoodsType } from '../../request/module.js'
import { sendServerGet } from '../../request/common.js'
Page({
  data: {
    typeList:[],
    // tempfilePath:'http://localhost:8080/img/',
    tempfilePath:'http://192.168.0.103:8080/img/',
    goodsList:[],
    searchname:"",
    typeId:"all",//商品分类的Id
    pageNum:1,
    pageSize:10
  },

  onLoad: function (options) {
    getGoodsType().then((res)=>{
      console.log(res)
      let a=res.extend.list
      a.unshift({id:'all',goodstypename:'全部商品'})
      this.setData({
        typeList:a
      });
    })
    this.getGoodsList();
  },
  onClick(e){
    console.log(e)
    let typeId=this.data.typeList[e.detail.index].id
    this.setData({
      typeId:typeId,
      goodsList:[],
      pageNum:1,
      pageSize:10
    })
    this.getGoodsList()
  },
  async getGoodsList(){
    console.log(111)
    let params={},send={}
    send.searchname=this.data.searchname;
    let typeId=this.data.typeId
    if(typeId=='all'){
      typeId='';
    }
    send.typeId=typeId;
    send.pageNum=this.data.pageNum;
    send.pageSize=this.data.pageSize;
    params.send=send
    console.log(send)

    params.url="goods/getsearchGoods"
    sendServerGet(params,(commRes)=>{
      console.log(commRes)
      if(commRes.code==100){
        const total=commRes.extend.pageInfo.total;
        this.totalPages=Math.ceil(total/this.data.pageSize);
        this.setData({
          goodsList:[...this.data.goodsList,...commRes.extend.pageInfo.list]
        })
      }
    })
    wx.stopPullDownRefresh();
      
  },
   // 页面上滑 滚动条触底事件
   onReachBottom(){
    //  1 判断还有没有下一页数据
      if(this.data.pageNum>=this.totalPages){
        // 没有下一页数据
        //  console.log('%c'+"没有下一页数据","color:red;font-size:100px;background-image:linear-gradient(to right,#0094ff,pink)");
        wx.showToast({ title: '没有下一页数据' });
          
      }else{
        // 还有下一页数据
        //  console.log('%c'+"有下一页数据","color:red;font-size:100px;background-image:linear-gradient(to right,#0094ff,pink)");
        this.data.pageNum++;
        this.getGoodsList();
      }
    },
    // 下拉刷新事件 
    onPullDownRefresh(){
      // 1 重置数组
      this.setData({
        goodsList:[]
      })
      // 2 重置页码
      this.data.pageNum=1;
      // 3 发送请求
      this.getGoodsList();
    }

})